$(function(){
	var start = {
	    format: 'YYYY-MM-DD hh:mm:ss',
	    minDate: "1970-01-01 00:00:00", //设定最小日期为当前日期
	    festival:false,
	    isinitVal:true,
	    hmsSetVal:{hh:00,mm:00,ss:00},
	    maxDate: "2099-12-31 23:59:59" //最大日期
	};
	var end = {
	    format: 'YYYY-MM-DD hh:mm:ss',
	    minDate: "1970-01-01 00:00:00", //设定最小日期为当前日期
	    festival:false,
	    isinitVal:true,
	    hmsSetVal:{hh:23,mm:59,ss:59},
	    maxDate: "2099-12-31 23:59:59" //最大日期
	};
	$("#starttime_calendar_history").jeDate(start);
	$("#endtime_calendar_history").jeDate(end);
})
function getBeforeFormatDate() {
    var date = new Date(new Date().getTime() - 86400000);
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}
function getNowFormatDate() {
    var date = new Date(new Date().getTime());
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}
//获取输入框中选中文本内容
function getFieldSelection(select_field){
     word='';
     if (document.selection) {
         var sel = document.selection.createRange();
         if (sel.text.length > 0) {
             word = sel.text;
         }
     }
     else if (select_field.selectionStart || select_field.selectionStart == '0') {
         var startP = select_field.selectionStart;
         var endP = select_field.selectionEnd;
         if (startP != endP) {
             word = select_field.value.substring(startP, endP);
         }
     }
     return word;
}

function KeyDown(obj,key,id){
	var selected_word = getFieldSelection(document.getElementById(id));
	key = (key) ? key : ((window.event) ? window.event : "");
	var keycode = key.keyCode?key.keyCode:key.which;
	if(!((keycode>=48 && keycode<=57)||(keycode>=96 && keycode<=105)||(keycode>=37 && keycode<=40))){
		key.preventDefault();
	}
	if((key.ctrlKey || key.altKey || key.shiftKey)){
		key.preventDefault();
	}
    if(selected_word.indexOf("-") > -1 || selected_word.indexOf(" ") > -1 || selected_word.indexOf(":") > -1 ){
    	key.preventDefault();
    }else{
    	$(obj).parent().removeClass("placeholder");
		if((keycode>=48 && keycode<=57)||(keycode>=96 && keycode<=105)){
			if($(obj).val().length < 5){
				$(obj).val($(obj).val().replace("-","").replace("-","").replace(" ","").replace(":","").replace(":",""));
			}else if($(obj).val().length == 4){
				if($(obj).val().substring(4,5) != "-" && $(obj).val().length > 3){
					$(obj).val($(obj).val().substring(0,4) + "-");
				}
			}else if($(obj).val().length == 5){
				if($(obj).val().substring(4,5) != "-"){
					$(obj).val($(obj).val().substring(0,4) + "-" + $(obj).val().substring(4));
				}
				if($(obj).val().substring(5,6) > 1){
					$(obj).val($(obj).val().substring(0,5) + "0" + $(obj).val().substring(5,6));
				}
				if($(obj).val().length == 7){
					if($(obj).val().substring(7,8) != "-" && $(obj).val().length > 6){
						$(obj).val($(obj).val().substring(0,7) + "-");
					}
				}
			}else if($(obj).val().length == 6){
				if($(obj).val().substring(4,5) != "-"){
					$(obj).val($(obj).val().substring(0,4) + "-" + $(obj).val().substring(4));
				}
				if($(obj).val().substring(5,6) > 1){
					$(obj).val($(obj).val().substring(0,5) + "0" + $(obj).val().substring(5,6));
				}
				if($(obj).val().length == 7){
					if($(obj).val().substring(7,8) != "-" && $(obj).val().length > 6){
						$(obj).val($(obj).val().substring(0,7) + "-");
					}
				}
			}else if($(obj).val().length == 7){
				if($(obj).val().substring(5,7) == "00"){
					$(obj).val($(obj).val().substring(0,5) + "01");
				}
				if($(obj).val().substring(5,7) > 12){
					$(obj).val($(obj).val().substring(0,5) + "0" + $(obj).val().substring(5,6));
				}
				if($(obj).val().substring(7,8) != "-" && $(obj).val().length > 6){
					$(obj).val($(obj).val().substring(0,7) + "-");
				}
			}else if($(obj).val().length == 9){
				if($(obj).val().substring(8,9) > 3){
					$(obj).val($(obj).val().substring(0,8) + "0" + $(obj).val().substring(8,9));
				}
				if($(obj).val().length == 10){
					if($(obj).val().substring(10,11) != " " && $(obj).val().length > 9){
						$(obj).val($(obj).val().substring(0,10) + " ");
					}
				}
			}else if($(obj).val().length == 10){
				if($(obj).val().substring(8,10) == "00"){
					$(obj).val($(obj).val().substring(0,8) + "01");
				}
				if($(obj).val().substring(8,10) > 31){
					$(obj).val($(obj).val().substring(0,8) + "0" + $(obj).val().substring(8,9));
				}
				if($(obj).val().substring(10,11) != " " && $(obj).val().length > 9){
					$(obj).val($(obj).val().substring(0,10) + " ");
				}
			}else if($(obj).val().length == 11){
				$(obj).val($(obj).val().substring(0,10) + " " + $(obj).val().substring(10,11));
			}else if($(obj).val().length == 12){
				if($(obj).val().substring(11,12) > 2){
					$(obj).val($(obj).val().substring(0,11) + "0" + $(obj).val().substring(11,12));
				}
				if($(obj).val().length == 13){
					if($(obj).val().substring(13,14) != ":" && $(obj).val().length > 12){
						$(obj).val($(obj).val().substring(0,13) + ":");
					}
				}
			}else if($(obj).val().length == 13){
				if($(obj).val().substring(11,13) > 23){
					$(obj).val($(obj).val().substring(0,11) + "0" + $(obj).val().substring(11,12));
				}
				if($(obj).val().substring(13,14) != ":" && $(obj).val().length > 12){
					$(obj).val($(obj).val().substring(0,13) + ":");
				}
			}else if($(obj).val().length == 15){
				if($(obj).val().substring(0,4).indexOf("-") > -1){
				}else{
					if($(obj).val().substring(14,15) > 5){
						$(obj).val($(obj).val().substring(0,14) + "0" + $(obj).val().substring(14,15));
					}
					if($(obj).val().length == 16){
						if($(obj).val().substring(16,17) != ":" && $(obj).val().length > 15){
							$(obj).val($(obj).val().substring(0,16) + ":");
						}
					}
				}
			}else if($(obj).val().length == 16){
				if($(obj).val().substring(0,4).indexOf("-") > -1 || $(obj).val().substring(0,4).indexOf(" ") > -1 || $(obj).val().substring(0,4).indexOf(":") > -1){
				}else{
					if($(obj).val().substring(14,16) > 59){
						$(obj).val($(obj).val().substring(0,14) + "0" + $(obj).val().substring(14,15));
					}
					if($(obj).val().substring(16,17) != ":" && $(obj).val().length > 15){
						$(obj).val($(obj).val().substring(0,16) + ":");
					}
				}
			}else if($(obj).val().length == 18){
				if($(obj).val().substring(0,4).indexOf("-") > -1 || $(obj).val().substring(0,4).indexOf(" ") > -1 || $(obj).val().substring(0,4).indexOf(":") > -1){
				}else{
					if($(obj).val().substring(5,6) > 1){
						$(obj).val($(obj).val().substring(0,5) + "0" + $(obj).val().substring(5,6) + $(obj).val().substring(6));
						$(obj).blur();
					}
					if($(obj).val().substring(5,6) < 2 && $(obj).val().substring(0,7).split("-").length > 2){
					}else{
						if($(obj).val().substring(5,7) == "02"){
							if($(obj).val().substring(8,9) > 2){
								$(obj).val($(obj).val().substring(0,8) + "0" + $(obj).val().substring(8,9) + $(obj).val().substring(9));
								$(obj).blur();
							}
						}
						if($(obj).val().substring(8,9) > 3){
							$(obj).val($(obj).val().substring(0,8) + "0" + $(obj).val().substring(8,9) + " " + $(obj).val().substring(10));
							$(obj).blur();
						}
						if($(obj).val().substring(8,9) < 4 && $(obj).val().substring(0,10).indexOf(" ") > -1){
						}else{
							if($(obj).val().substring(11,12) > 2){
								$(obj).val($(obj).val().substring(0,11) + "0" + $(obj).val().substring(11,12) + $(obj).val().substring(12));
								$(obj).blur();
							}
							if($(obj).val().substring(11,12) < 3 && $(obj).val().substring(0,13).indexOf(":") > -1){
							}else{
								if($(obj).val().substring(14,15) > 5){
									$(obj).val($(obj).val().substring(0,14) + "0" + $(obj).val().substring(14,15) + $(obj).val().substring(15));
									$(obj).blur();
								}
								if($(obj).val().substring(14,15) < 6 && $(obj).val().substring(0,16).split(":").length > 2){
								}else{
									if($(obj).val().substring(17,18) > 5){
										$(obj).val($(obj).val().substring(0,17) + "0" + $(obj).val().substring(17,18));
										$(obj).blur();
									}
								}
							}
						}
					}
				}
			}
		}
    }
}
function KeyUp(obj){
	if($(obj).val().length == 19){
		if($(obj).val().substring(5,7) > 12){
			$(obj).val($(obj).val().substring(0,5) + "0" + $(obj).val().substring(5,6) + $(obj).val().substring(7));
			$(obj).blur();
		}else if($(obj).val().substring(5,7) == "02"){
			var yearR = $(obj).val().substring(0,4);
			if((yearR % 100 !== 0 && yearR % 4 === 0) || (yearR % 400 === 0)){
			}else{
				if($(obj).val().substring(8,10) > 28){
					$(obj).val($(obj).val().substring(0,8) + "0" + $(obj).val().substring(8,9) + $(obj).val().substring(10));
					$(obj).blur();
				}
			}
		}else if($(obj).val().substring(5,7) == "01" || $(obj).val().substring(5,7) == "03" || $(obj).val().substring(5,7) == "05" || $(obj).val().substring(5,7) == "07" || $(obj).val().substring(5,7) == "08" || $(obj).val().substring(5,7) == "10" ||$(obj).val().substring(5,7) == "12"){
			if($(obj).val().substring(8,10) > 31){
				$(obj).val($(obj).val().substring(0,8) + "0" + $(obj).val().substring(8,9) + $(obj).val().substring(10));
				$(obj).blur();
			}
		}else{
			if($(obj).val().substring(8,10) > 30){
				$(obj).val($(obj).val().substring(0,8) + "0" + $(obj).val().substring(8,9) + $(obj).val().substring(10));
				$(obj).blur();
			}
		}
		if($(obj).val().substring(11,13) > 23){
			$(obj).val($(obj).val().substring(0,11) + "0" + $(obj).val().substring(11,12) + $(obj).val().substring(13));
			$(obj).blur();
		}
	}
}
//光标失去焦点校验
function checkDateoth(obj,str,str2,str3){
	var begin_time = $(obj).val();
	var start = checkDate(begin_time);
	if(begin_time == ""){
		shows(str2,3000);
	}else if(!start){
		if(begin_time.substring(0,4) > 1970 && begin_time.substring(0,4) < 2099){
			shows(str3,3000);
		}else{
			shows(str,3000);
		}
	}
}
